#pragma once

#include "CTP/ThostFtdcMdApi.h"
#include "TrManager.h"
#include <thread>

#pragma warning(disable:4312)

#pragma comment(lib, "TrManager.lib")
#pragma comment(lib, "TrUtils.lib")

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif


#ifndef BUILD_STATIC
# if defined(TRMARKET_LIB)
#  define TRMARKET_EXPORT __declspec(dllexport)
# else
#  define TRMARKET_EXPORT __declspec(dllimport)
# endif
#else
# define TRMARKET_EXPORT
#endif


class TRMARKET_EXPORT TrMarket : public CThostFtdcMdSpi
{
public:
    TrMarket(const TrConf_CTP& ctp, const TrConf_Log& log);
    ~TrMarket();
    void start();
    void stop();
    void add(const std::string& code);
    void filter(const std::vector<std::string>& list);
    void release();
    bool status();
    void attach(TrMdObserver* observer, const std::string& code, TrPeriod period);
    static std::vector<WTSBarStruct> quote(const std::string& code, int period = 0);
    //============================================================
    virtual void OnFrontConnected() override;
    virtual void OnFrontDisconnected(int nReason) override;
    virtual void OnHeartBeatWarning(int nTimeLapse) override;
    virtual void OnRspUserLogin(CThostFtdcRspUserLoginField* pRspUserLogin, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspUserLogout(CThostFtdcUserLogoutField* pUserLogout, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspError(CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspSubMarketData(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspUnSubMarketData(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspSubForQuoteRsp(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRspUnSubForQuoteRsp(CThostFtdcSpecificInstrumentField* pSpecificInstrument, CThostFtdcRspInfoField* pRspInfo, int nRequestID, bool bIsLast) override;
    virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField* pDepthMarketData) override;
    virtual void OnRtnForQuoteRsp(CThostFtdcForQuoteRspField* pForQuoteRsp) override;
};
